<template>
	<view class="order-manage-container">
		<!-- 顶部标签栏 -->
		<view class="tab-header">
			<view 
				v-for="(tab, index) in tabs" 
				:key="index" 
				class="tab-item" 
				:class="{ active: currentTab === tab.value }"
				@click="switchTab(tab.value)"
			>
				{{ tab.label }}
				<view class="tab-indicator" v-if="currentTab === tab.value"></view>
			</view>
		</view>
		
		<!-- 订单列表 -->
		<view class="order-list" v-if="orderList.length > 0">
			<view class="order-card" v-for="(item, index) in orderList" :key="item.id" @click="viewOrderDetail(item.id)">
				<view class="order-header">
					<view class="order-id">订单号: {{ item.id }}</view>
					<view class="order-status" :class="getStatusClass(item.status)">{{ getStatusText(item.status) }}</view>
				</view>
				
				<view class="order-title">{{ item.title }}</view>
				<view class="order-content">{{ item.content }}</view>
				
				<view class="order-info">
					<view class="info-item">
						<text class="info-label">开发语言:</text>
						<text class="info-value">{{ item.language || '未指定' }}</text>
					</view>
					<view class="info-item">
						<text class="info-label">预算范围:</text>
						<text class="info-value">¥{{ item.lessBudget }} - ¥{{ item.highBudget }}</text>
					</view>
					<view class="info-item">
						<text class="info-label">创建时间:</text>
						<text class="info-value">{{ formatDate(item.createdTime) }}</text>
					</view>
				</view>
				
				<view class="order-actions">
					<view class="action-btn" :class="{ 'primary-btn': item.status === 'pending' }" @click.stop="handleOrder(item)">
						{{ item.status === 'pending' ? '处理订单' : '查看详情' }}
					</view>
				</view>
			</view>
		</view>
		
		<!-- 空状态 -->
		<view class="empty-state" v-else-if="!isLoading">
			<text class="empty-icon">📋</text>
			<text class="empty-text">暂无{{ getTabLabel(currentTab) }}订单数据</text>
		</view>
		
		<!-- 分页控件 -->
		<view class="pagination" v-if="orderList.length > 0">
			<view class="page-btn" :class="{ disabled: currentPage <= 1 }" @click="prevPage">
				<text class="page-arrow">←</text>
			</view>
			<view class="page-number">{{ currentPage }} / {{ totalPages }}</view>
			<view class="page-btn" :class="{ disabled: currentPage >= totalPages }" @click="nextPage">
				<text class="page-arrow">→</text>
			</view>
		</view>
		
		<!-- 加载状态 -->
		<view class="loading-state" v-if="isLoading">
			<view class="loading-spinner"></view>
			<text class="loading-text">加载中...</text>
		</view>
	</view>
</template>

<script setup>
import { ref, computed, onMounted, watch } from 'vue'
import request from '@/utils/request'

// 标签页配置
const tabs = [
	{ label: '全部', value: 'all' },
	{ label: '待处理', value: '待处理' },
	{ label: '处理中', value: '处理中' },
	{ label: '已完成', value: '已完成' },
	{ label: '已取消', value: '已取消' }
]

// 数据状态
const orderList = ref([])
const currentTab = ref('all')
const currentPage = ref(1)
const pageSize = ref(10)
const totalOrders = ref(0)
const totalPages = computed(() => Math.ceil(totalOrders.value / pageSize.value))
const isLoading = ref(false)

// 监听标签切换，重置页码并重新获取数据
watch(currentTab, () => {
	currentPage.value = 1
	fetchOrderList()
})

// 获取订单列表
const fetchOrderList = async () => {
	isLoading.value = true
	try {
		const status = currentTab.value === 'all' ? '' : currentTab.value
		
		const res = await request('/admin/order/list', {
			method: 'GET',
			data: {
				pageNum: currentPage.value,
				pageSize: pageSize.value,
				status: status
			}
		})
		
		if (res.code === 200) {
			orderList.value = res.data || []
		} else {
			uni.showToast({
				title: res.msg || '获取订单列表失败',
				icon: 'none'
			})
		}
	} catch (error) {
		console.error('获取订单列表失败', error)
		uni.showToast({
			title: '网络错误，请稍后重试',
			icon: 'none'
		})
	} finally {
		isLoading.value = false
	}
}

// 切换标签
const switchTab = (tab) => {
	currentTab.value = tab
}

// 上一页
const prevPage = () => {
	if (currentPage.value > 1) {
		currentPage.value--
		fetchOrderList()
	}
}

// 下一页
const nextPage = () => {
	if (currentPage.value < totalPages.value) {
		currentPage.value++
		fetchOrderList()
	}
}

// 查看订单详情
const viewOrderDetail = (orderId) => {
	uni.navigateTo({
		url: `/pages/admin/order-detail?id=${orderId}`
	})
}

// 处理订单
const handleOrder = (order) => {
	if (order.status === 'pending') {
		uni.navigateTo({
			url: `/pages/admin/order-process?id=${order.id}`
		})
	} else {
		viewOrderDetail(order.id)
	}
}

// 获取状态文本
const getStatusText = (status) => {
	const statusMap = {
		'pending': '待处理',
		'processing': '处理中',
		'completed': '已完成',
		'cancelled': '已取消'
	}
	return statusMap[status] || status
}

// 获取状态样式类
const getStatusClass = (status) => {
	return {
		'status-pending': status === 'pending',
		'status-processing': status === 'processing',
		'status-completed': status === 'completed',
		'status-cancelled': status === 'cancelled'
	}
}

// 获取当前标签文本
const getTabLabel = (tabValue) => {
	const tab = tabs.find(t => t.value === tabValue)
	return tab ? tab.label : '全部'
}

// 格式化日期
const formatDate = (dateString) => {
	if (!dateString) return '未知'
	const date = new Date(dateString)
	return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')} ${String(date.getHours()).padStart(2, '0')}:${String(date.getMinutes()).padStart(2, '0')}`
}

// 页面加载时获取订单列表
onMounted(() => {
	fetchOrderList()
})
</script>

<style lang="scss" scoped>
.order-manage-container {
	min-height: 100vh;
	padding: 30rpx;
	background: linear-gradient(135deg, #18122B 0%, #120C1C 100%);
	color: #fff;
}

// 标签栏
.tab-header {
	display: flex;
	justify-content: space-around;
	background: rgba(34, 26, 54, 0.7);
	border-radius: 16rpx;
	padding: 10rpx;
	margin-bottom: 30rpx;
	position: sticky;
	top: 0;
	z-index: 10;
	backdrop-filter: blur(10px);
	border: 1px solid #2D1457;
}

.tab-item {
	flex: 1;
	text-align: center;
	padding: 20rpx 0;
	font-size: 28rpx;
	color: #B8B8D1;
	position: relative;
	transition: all 0.3s;
}

.tab-item.active {
	color: #A259FF;
	font-weight: bold;
}

.tab-indicator {
	position: absolute;
	bottom: 0;
	left: 50%;
	transform: translateX(-50%);
	width: 40rpx;
	height: 4rpx;
	background: linear-gradient(90deg, #7B2FF2, #A259FF);
	border-radius: 4rpx;
}

// 订单卡片
.order-list {
	display: flex;
	flex-direction: column;
	gap: 30rpx;
	margin-bottom: 40rpx;
}

.order-card {
	background: linear-gradient(135deg, #221A36 0%, #18122B 100%);
	border-radius: 16rpx;
	padding: 30rpx;
	box-shadow: 0 4rpx 16rpx #7B2FF244;
	border: 1px solid #2D1457;
	transition: transform 0.2s;
}

.order-card:active {
	transform: scale(0.99);
}

.order-header {
	display: flex;
	justify-content: space-between;
	align-items: center;
	margin-bottom: 20rpx;
}

.order-id {
	font-size: 24rpx;
	color: #B8B8D1;
}

.order-status {
	padding: 6rpx 16rpx;
	border-radius: 30rpx;
	font-size: 24rpx;
	font-weight: bold;
}

.status-pending {
	background: rgba(255, 159, 0, 0.2);
	color: #FF9F00;
}

.status-processing {
	background: rgba(16, 174, 255, 0.2);
	color: #10AEFF;
}

.status-completed {
	background: rgba(76, 217, 100, 0.2);
	color: #4CD964;
}

.status-cancelled {
	background: rgba(255, 77, 79, 0.2);
	color: #FF4D4F;
}

.order-title {
	font-size: 32rpx;
	font-weight: bold;
	color: #fff;
	margin-bottom: 16rpx;
}

.order-content {
	font-size: 26rpx;
	color: #B8B8D1;
	margin-bottom: 24rpx;
	display: -webkit-box;
	-webkit-line-clamp: 2;
	-webkit-box-orient: vertical;
	overflow: hidden;
	text-overflow: ellipsis;
}

.order-info {
	background: rgba(24, 18, 43, 0.5);
	border-radius: 12rpx;
	padding: 20rpx;
	margin-bottom: 24rpx;
}

.info-item {
	display: flex;
	margin-bottom: 10rpx;
}

.info-item:last-child {
	margin-bottom: 0;
}

.info-label {
	font-size: 24rpx;
	color: #B8B8D1;
	width: 160rpx;
}

.info-value {
	font-size: 24rpx;
	color: #fff;
	flex: 1;
}

.order-actions {
	display: flex;
	justify-content: flex-end;
}

.action-btn {
	padding: 15rpx 30rpx;
	border-radius: 12rpx;
	font-size: 26rpx;
	background: rgba(123, 47, 242, 0.2);
	color: #A259FF;
	border: 1px solid #2D1457;
}

.primary-btn {
	background: linear-gradient(45deg, #7B2FF2, #10AEFF);
	color: #fff;
	border: none;
}

// 分页控件
.pagination {
	display: flex;
	justify-content: center;
	align-items: center;
	margin-top: 30rpx;
}

.page-btn {
	width: 80rpx;
	height: 80rpx;
	background: #221A36;
	border-radius: 12rpx;
	display: flex;
	align-items: center;
	justify-content: center;
	margin: 0 20rpx;
	border: 1px solid #2D1457;
}

.page-btn.disabled {
	opacity: 0.5;
	pointer-events: none;
}

.page-arrow {
	font-size: 32rpx;
	color: #A259FF;
}

.page-number {
	font-size: 28rpx;
	color: #B8B8D1;
}

// 空状态
.empty-state {
	padding: 80rpx 0;
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
}

.empty-icon {
	font-size: 80rpx;
	margin-bottom: 20rpx;
	color: #2D1457;
}

.empty-text {
	font-size: 28rpx;
	color: #B8B8D1;
}

// 加载状态
.loading-state {
	position: fixed;
	top: 0;
	left: 0;
	right: 0;
	bottom: 0;
	background: rgba(24, 18, 43, 0.7);
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	z-index: 999;
}

.loading-spinner {
	width: 80rpx;
	height: 80rpx;
	border: 6rpx solid #2D1457;
	border-top: 6rpx solid #A259FF;
	border-radius: 50%;
	animation: spin 1s linear infinite;
	margin-bottom: 20rpx;
}

.loading-text {
	font-size: 28rpx;
	color: #B8B8D1;
}

@keyframes spin {
	0% { transform: rotate(0deg); }
	100% { transform: rotate(360deg); }
}
</style>